package com.ourteam.product.requirement.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.product.requirement.dao.ProductRequirementDAOQueryBean;
import com.ourteam.product.requirement.service.IProductService;
import com.ourteam.product.requirement.service.ProductServiceFactory;

@Controller
@RequestMapping("/api/PRODUCT/REQ/ProductRequirement")
public class ProductRequirementAPI extends AbstractServiceController {

	private static final IProductService PRODUCT_SERVICE = ProductServiceFactory.getProductService();

	@RequestMapping("/queryProductRequirements")
	@ResponseBody
	public GridBean queryProductRequirements(@ModelAttribute ProductRequirementSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductRequirementDAOQueryBean, ProductRequirementSearchModel> searchHelper = new AbstractSearchHelper<ProductRequirementDAOQueryBean, ProductRequirementSearchModel>(
				searchModel) {

			@Override
			protected int queryCount(ProductRequirementDAOQueryBean queryBean) throws Exception {
				return PRODUCT_SERVICE.queryProductRequirementCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductRequirementDAOQueryBean queryBean) throws Exception {
				return PRODUCT_SERVICE.queryProductRequirements(queryBean);
			}

			@Override
			protected ProductRequirementDAOQueryBean createQueryBean() {
				return new ProductRequirementDAOQueryBean();
			}

		};

		return searchHelper.doSearch(request);

	}

}
